/*
 *   Notepasser, an insecure, outdated, XOR-encryption based cryptographic tool;
 *   Copyright (C) 2011 Brady Itkin
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ------------------------------------------------------------------------
 * WARNING- this isn't for regular encryption, this is just 
 * for passing notes that you want some level of privacy for
 * notes to pass around a classroom
 * but will not matter as much as, say credit card information
 * or the nuclear bomb codes.
 * I am not responsible for your stupidity to use this program
 * for those types of things.
 */
//TODO: Get rid of onetimepad reliance, make it more like a vigenere?
#define ARRAYSIZE 256
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

string caesar(string inpt,int sft)
{
string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-!@#$%^&*()_= ";
int lettercodes [inpt.length()];
for(int pos=0;pos<inpt.length(); pos++)
{
lettercodes[pos] = sft + letters.find(inpt.substr(pos,1));
if(lettercodes[pos]>letters.length())
   lettercodes[pos] = sft + lettercodes[pos]-letters.length(); //TODO: comment this stuff
}
int len = inpt.length();
inpt = "";
for(int pos=0;pos<len;pos++)
{
inpt = inpt + letters.substr(lettercodes[pos],1);        
}
return inpt;
}


int main(int argc, char *argv[]) {   
	char ciphertext[ARRAYSIZE];
  char key[ARRAYSIZE];
  char plaintext[ARRAYSIZE];
	char exitchar;	//due to stacksmashing problems... still doesn't help
	char useroptionin;
    cout << "\n[E]ncrypt or [D]ecrypt? >>>";
    cin >> useroptionin;
    if (useroptionin == 'E' || useroptionin == 'e') {
                    cin.ignore(ARRAYSIZE,'\n'); //IMPORTANT!!!! 
                    ofstream outfile;					
                    int x = 0;               
                    cout << "\n Enter the plaintext you will encrypt >>>";
                    //cin >> plaintext;
                    cin.getline(plaintext, ARRAYSIZE);
                    cout << "\n Enter your key (must be as long as plaintext)>>>";
                    //cin >> key;
                    cin.getline(key, ARRAYSIZE);
					while (x <= ARRAYSIZE) {
                        ciphertext[x] = plaintext[x] ^ key[x];
                        x++;
                     }
                     cout << "\n Your cipher text is: \n" << ciphertext;
					 cout << "Opening outstream to cipher.txt...\n";
					 outfile.open("cipher.txt", ios::trunc | ios::out);
					 if (outfile.is_open()) {
						 cout << "Writing ciphertext to file.....\n";
						 outfile << ciphertext;
						 cout << "Success!";
					 }
					 else { 
						 cerr << "Could not open stream to cipher.txt -- try running with sudo?"; 
					 }
                     cin.get(exitchar); //TODO: get rid of stacksmashing at this point
                     return 0;
    }
    if (useroptionin == 'D' || useroptionin == 'd') {
                     ifstream infile;
					 int x = 0;
                     cin.ignore(ARRAYSIZE, '\n');
					 cout << "\nOpening stream to cipher.txt...";
					 infile.open("cipher.txt");
					 if (infile.is_open()) {
						 cout << "\nNow reading ciphertext from cipher.txt....";
						 infile >> ciphertext;
						 cout << "\nSuccess!";
					 }
					 else {
						 cerr << "Could not open up stream to cipher.txt -- do I have permissions?";
						 cin.get(exitchar);
						 return 1;
					 }
					 //cout << "\nEnter the ciphertext >>>";
                     //cin >> ciphertext;
                     //cin.getline(ciphertext, ARRAYSIZE);
                     cout << "\nEnter your key >>>";
                     //cin >> key;
                     cin.getline(key, ARRAYSIZE);
                     while (x <= ARRAYSIZE) {
                           plaintext[x] = ciphertext[x] ^ key[x];
                           x++;
                           }
                     cout << "\n Your plaintext is: \n" << plaintext;
                     cin.get(exitchar);
                     return 0;
    }
    cerr << "Error: Detected unexpected input, exitting...";
	cin.get();
	return 1;
}
